package com.thinkgem.jeesite.modules.book.entity;

import com.thinkgem.jeesite.common.persistence.DataEntity;
import com.thinkgem.jeesite.common.utils.excel.annotation.ExcelField;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * BkBookinfo
 * Created by aulbrother on 2015/11/4.
 */
public class BkBookinfo extends DataEntity<BkBookinfo>{

    private static final long serialVersionUID = 3909880438477662515L;

    private String id;
    private String bookname;
    private String pinyin;
    private String othername;
    private String author;
    private String edition;
    private String isbn;
    private String pubplace;
    private String publisher;
    private String pubdate;
    private String pages;
    private String appendix;
    private String user;
    private String classcode;
    private String className;
    private String version;
    private String price;
    private String books;
    private String booksNa;
    private String booksNum;
    private String booksauthor;
    private String endnote;
    private String docclass;
    private String measure;
    private String ztc;
    private String lagclass;
    private String marc;
    private String cbbh;
    private String tubiao;
    private String issn;
    private String cnissn;
    private String zdcode;
    private String remarks;
    // 主题
    private String theme;

    private BkBookstore bookstore;

    private BkType bkType;

    public BkBookinfo(){}

    public BkBookinfo(String marc, Map<String, CNMARC> cnmarcMap){
        this.marc = marc;
        for(String key : cnmarcMap.keySet()){
            //
            if("LDR".equals(key)){
                cnmarcMap.get(key);
            }
            // 记录控制号
            else if("001".equals(key)){
                CNMARC fullId = cnmarcMap.get(key);
                String content = fullId.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                this.id = tem[0];
            }
            // 记录版次标识
            else if("005".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                this.edition = tem[0];
            }
            // 国际标准书号(ISBN)
            else if("010".equals(key)){
                CNMARC fullISBN = cnmarcMap.get(key);
                String content = fullISBN.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.isbn = item.substring(1).replaceAll("-","");
                    }
                    else if(item.startsWith("d")){
                        this.price = item.substring(1);
                        if(this.price.startsWith("CNY") || this.price.startsWith("RMB") || this.price.startsWith("rmb")){
                            this.price = this.price.substring(3);
                        }
                    }
                }
            }
            // 国际标准连续出版物号(ISSN)
            else if("011".equals(key)){
                CNMARC fullISSN = cnmarcMap.get(key);
                String content = fullISSN.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.issn = item.substring(1).replaceAll("-","");
                        break;
                    }
                }
            }
            // 国家书目号
            else if("020".equals(key)){
            }
            // 版权登记号
            else if("021".equals(key)){
            }
            // 政府出版物号
            else if("022".equals(key)){
            }
            else if("035".equals(key)){
                cnmarcMap.get(key);
            }
            // 统一书刊号
            else if("091".equals(key)){
                cnmarcMap.get(key);
            }
            // 一般数据处理（如入档日期、出版日期、阅读对象代码等）
            else if("100".equals(key)){
                cnmarcMap.get(key);
            }
            // 作品语种
            else if("101".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.lagclass = item.substring(1);
                    }
                    else if(item.startsWith("e")){

                    }
                }
            }
            // 出版国别
            else if("102".equals(key)){
                cnmarcMap.get(key);
            }
            // 编码数据字段：图书（如图表代码、内容特征代码、会议代码等）
            else if("105".equals(key)){
                cnmarcMap.get(key);
            }
            // 文字资料的形态特征
            else if("106".equals(key)){
                cnmarcMap.get(key);
            }
            // 题名与责任说明项
            else if("200".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.bookname = item.substring(1);
                    }
                    else if(item.startsWith("f")){
                        this.author = item.substring(1);
                    }
                    else if(item.startsWith("9") || item.startsWith("A")){
                        this.pinyin = item.substring(1);
                    }
                    else if(item.startsWith("e")){
                        this.othername = item.substring(1);
                    }
                }

            }
            // 版本项
            else if("205".equals(key)){
                cnmarcMap.get(key);
            }
            // 出版发行项
            else if("210".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.pubplace = item.substring(1);
                    }
                    else if(item.startsWith("c")){
                        this.publisher = item.substring(1);
                    }
                    else if(item.startsWith("d")){
                        this.pubdate = item.substring(1);
                    }
                }
            }
            // 载体形态项
            else if("215".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.pages = item.substring(1);
                    }
                    else if(item.startsWith("c")){
                        this.ztc = item.substring(1);
                    }
                    else if(item.startsWith("d")){
                        this.measure = item.substring(1);
                    }
                    else if(item.startsWith("e")){
                        this.appendix = item.substring(1);
                    }
                }
            }
            // 丛编项
            else if("225".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.books = item.substring(1);
                    }
                    else if(item.startsWith("i")){
                        this.booksNa = item.substring(1);
                    }
                    else if(item.startsWith("f")){
                        this.booksauthor = item.substring(1);
                    }
                    else if(item.startsWith("v")){
                        this.booksNum = item.substring(1);
                    }
                }
            }
            // 一般性附注
            else if("300".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.endnote = item.substring(1);
                    }
                }
            }
            // 题名与责任者附注
            else if("303".equals(key)){
                cnmarcMap.get(key);
            }
            // 题名与责任者附注
            else if("304".equals(key)){
                cnmarcMap.get(key);
            }
            // 版本沿革附注
            else if("305".equals(key)){
                cnmarcMap.get(key);
            }
            // 出版发行项附注
            else if("306".equals(key)){
                cnmarcMap.get(key);
            }
            // 载体形态项附注
            else if("307".equals(key)){
                cnmarcMap.get(key);
            }
            //  装订与获得方式附注
            else if("310".equals(key)){
                cnmarcMap.get(key);
            }
            // 书目附注
            else if("320".equals(key)){
                cnmarcMap.get(key);
            }
            // 内容附注
            else if("327".equals(key)){
                cnmarcMap.get(key);
            }
            // 学位论文附注
            else if("328".equals(key)){
                cnmarcMap.get(key);
            }
            // 提要或文摘
            else if("330".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.remarks = item.substring(1);
                    }
                }
            }
            // 使用对象附注
            else if("333".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.user = item.substring(1);
                    }
                }
            }
            // 丛编
            else if("410".equals(key)){
                cnmarcMap.get(key);
            }
            // 附属丛编
            else if("411".equals(key)){
                cnmarcMap.get(key);
            }
            // 统一题名
            else if("500".equals(key)){
                cnmarcMap.get(key);
            }
            // 作品集统一题名
            else if("501".equals(key)){
                cnmarcMap.get(key);
            }
            //  统一惯用标目
            else if("503".equals(key)){
                cnmarcMap.get(key);
            }
            // 并列题名
            else if("510".equals(key)){
                cnmarcMap.get(key);
            }
            // 封面题名
            else if("512".equals(key)){
                cnmarcMap.get(key);
            }
            // 附加题名页题名
            else if("513".equals(key)){
                cnmarcMap.get(key);
            }
            // 逐页题名
            else if("515".equals(key)){
                cnmarcMap.get(key);
            }
            // 普通主题
            else if("606".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                this.theme = "";
                for(String item : tem){
                    if(StringUtils.isNotEmpty(item)){
                        item = item.substring(1);
                        if(StringUtils.isNotBlank(item)){
                            this.theme += item.substring(1) + ",";
                        }
                    }
                }
                if(theme.endsWith(",")){
                    this.theme = theme.substring(0, theme.length()-1);
                }
            }
            // 《中图法》分类号
            else if("690".equals(key)){
                CNMARC fullName = cnmarcMap.get(key);
                String content = fullName.getContent().replace(CNMARC.RS, "");
                String[] tem = content.split(CNMARC.US);
                for(String item : tem){
                    if(item.startsWith("a")){
                        this.classcode = item.substring(1);
                    }else if(item.startsWith("x")){
                        /*this.classcode = item.substring(1);*/
                    }else if(item.startsWith("v")){
                        this.version = item.substring(1);
                    }
                }
            }
            // 记录来源
            else if("801".equals(key)){
                cnmarcMap.get(key);
            }
            // 馆藏信息
            else if("905".equals(key)){
                cnmarcMap.get(key);
            }
        }
    }

    public BkBookinfo(Map<String, Object> map){
        this.id = (String)map.get("id");
        this.bookname = (String)map.get("title");
        this.pinyin = (String)map.get("pinyin");
        this.othername = (String)map.get("othername");
        List<String> authors = (List)map.get("author");
        String author = "";
        for(String auth : authors){
            author += auth + ",";
        }
        if(author.endsWith(",")){
            this.author = author.substring(0, author.length() - 1);
        }

        this.edition = (String)map.get("edition");
        this.isbn = (String)map.get("isbn13");
        this.pubplace = (String)map.get("pubplace");
        this.publisher = (String)map.get("publisher");
        this.pubdate = (String)map.get("pubdate");
        this.pages = (String)map.get("pages");
        this.appendix = (String)map.get("appendix");
        this.user = (String)map.get("user");
        this.classcode = (String)map.get("classcode");
        this.className = (String)map.get("className");
        this.version = (String)map.get("version");
        this.price = ((String)map.get("price")).replace("元", "");
        this.books = (String)map.get("books");
        this.booksNa = (String)map.get("booksNa");
        this.booksNum = (String)map.get("booksNum");
        this.booksauthor = (String)map.get("booksauthor");
        this.endnote = (String)map.get("catalog");
        this.docclass = (String)map.get("docclass");
        this.measure = (String)map.get("measure");
        this.ztc = (String)map.get("ztc");
        this.lagclass = (String)map.get("lagclass");
        this.marc = (String)map.get("marc");
        this.cbbh = (String)map.get("cbbh");
        this.tubiao = (String)map.get("image");
        this.issn = (String)map.get("issn");
        this.cnissn = (String)map.get("cnissn");
        this.zdcode = (String)map.get("zdcode");
        this.remarks = (String)map.get("summary");
        List<Map<String,String>> tags = (List) map.get("tags");
        String theme = "";
        for(Map<String, String> m : tags){
            theme += m.get("name") + ",";
        }
        if(theme.endsWith(",")){
            this.theme = theme.substring(0, theme.length() - 1);
        }
        //this.theme = (String)map.get("tags");

    }

    public String getId(){
        return id;
    }

    public void setId(String id){
        this.id = id;
    }

    @ExcelField(title="正题名", type=0, sort=1)
    public String getBookname(){
        return bookname;
    }

    @ExcelField(title="ISBN", type=0, sort=2)
    public String getIsbn(){
        return isbn;
    }

    @ExcelField(title="副题名", type=0, sort=3)
    public String getOthername(){
        return othername;
    }

    @ExcelField(title="丛编题名", type=0, sort=5)
    public String getBooks(){
        return books;
    }

    @ExcelField(title="编著者", type=0, sort=6)
    public String getAuthor(){
        return author;
    }

    @ExcelField(title="分类号", type=0, sort=7)
    public String getClasscode(){
        return classcode;
    }

    @ExcelField(title="分类名", type=0, sort=8)
    public String getClassName(){
        return className;
    }

    @ExcelField(title="出版社", type=0, sort=9)
    public String getPublisher(){
        return publisher;
    }

    @ExcelField(title="出版地", type=0, sort=10)
    public String getPubplace(){
        return pubplace;
    }

    @ExcelField(title="出版日期", type=0, sort=11)
    public String getPubdate(){
        return pubdate;
    }

    @ExcelField(title="页码", type=0, sort=12)
    public String getPages(){
        return pages;
    }

    @ExcelField(title="开本", type=0, sort=13)
    public String getMeasure(){
        return measure;
    }

    @ExcelField(title="价格", type=0, sort=14)
    public void setPrice(String price){
        this.price = price;
    }

    @ExcelField(title="语种", type=0, sort=15)
    public String getLagclass(){
        return lagclass;
    }

    @ExcelField(title="文献类别", type=0, sort=16)
    public String getZtc(){
        return ztc;
    }

    @ExcelField(title="版次", type=0, sort=17)
    public String getVersion(){
        return version;
    }

    @ExcelField(title="卷册号", type=0, sort=18)
    public String getBooksNum(){
        return booksNum;
    }

    @ExcelField(title="编目批次", type=0, sort=22)
    public String getEdition(){
        return edition;
    }

    @ExcelField(title="附件", type=0, sort=19)
    public String getAppendix(){
        return appendix;
    }

    @ExcelField(title="主题词", type=0, sort=20)
    public String getTheme(){
        return theme;
    }

    @ExcelField(title="题名首拼", type=0, sort=21)
    public String getPinyin(){
        return pinyin;
    }

    @ExcelField(title="摘要", type=0, sort=22)
    public String getRemarks(){
        return remarks;
    }



    public void setBookname(String bookname){
        this.bookname = bookname;
    }
    public void setPinyin(String pinyin){
        this.pinyin = pinyin;
    }
    public void setOthername(String othername){
        this.othername = othername;
    }
    public void setAuthor(String author){
        this.author = author;
    }
    public void setEdition(String edition){
        this.edition = edition;
    }
    public void setIsbn(String isbn){
        this.isbn = isbn;
    }
    public void setPubplace(String pubplace){
        this.pubplace = pubplace;
    }
    public void setPublisher(String publisher){
        this.publisher = publisher;
    }
    public void setPubdate(String pubdate){
        this.pubdate = pubdate;
    }
    public void setPages(String pages){
        this.pages = pages;
    }
    public void setAppendix(String appendix){
        this.appendix = appendix;
    }
    public String getUser(){
        return user;
    }
    public void setUser(String user){
        this.user = user;
    }
    public void setClasscode(String classcode){
        this.classcode = classcode;
    }
    public void setClassName(String className){
        this.className = className;
    }
    public void setVersion(String version){
        this.version = version;
    }
    public String getPrice(){
        return price;
    }
    public void setBooks(String books){
        this.books = books;
    }
    public String getBooksNa(){
        return booksNa;
    }
    public void setBooksNa(String booksNa){
        this.booksNa = booksNa;
    }
    public void setBooksNum(String booksNum){
        this.booksNum = booksNum;
    }
    public String getBooksauthor(){
        return booksauthor;
    }
    public void setBooksauthor(String booksauthor){
        this.booksauthor = booksauthor;
    }
    public String getEndnote(){
        return endnote;
    }
    public void setEndnote(String endnote){
        this.endnote = endnote;
    }
    public String getDocclass(){
        return docclass;
    }
    public void setDocclass(String docclass){
        this.docclass = docclass;
    }
    public void setMeasure(String measure){
        this.measure = measure;
    }
    public void setZtc(String ztc){
        this.ztc = ztc;
    }
    public void setLagclass(String lagclass){
        this.lagclass = lagclass;
    }
    public String getMarc(){
        return marc;
    }
    public void setMarc(String marc){
        this.marc = marc;
    }
    public String getCbbh(){
        return cbbh;
    }
    public void setCbbh(String cbbh){
        this.cbbh = cbbh;
    }
    public String getTubiao(){
        return tubiao;
    }
    public void setTubiao(String tubiao){
        this.tubiao = tubiao;
    }
    public String getIssn(){
        return issn;
    }
    public void setIssn(String issn){
        this.issn = issn;
    }
    public String getCnissn(){
        return cnissn;
    }
    public void setCnissn(String cnissn){
        this.cnissn = cnissn;
    }
    public String getZdcode(){
        return zdcode;
    }
    public void setZdcode(String zdcode){
        this.zdcode = zdcode;
    }
    public void setRemarks(String remarks){
        this.remarks = remarks;
    }
    public void setTheme(String theme){
        this.theme = theme;
    }
    public BkBookstore getBookstore(){
        return bookstore;
    }
    public void setBookstore(BkBookstore bookstore){
        this.bookstore = bookstore;
    }
    public BkType getBkType(){
        return bkType;
    }
    public void setBkType(BkType bkType){
        this.bkType = bkType;
    }
}
